package com.dlwx.parttimetalent.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.bean.NearByInfoBean;
import com.dlwx.parttimetalent.inter.OnRecyclerViewItemClickListener;
import com.dlwx.parttimetalent.utils.DisplayUtil;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Administrator on 2017/11/21.
 */

public class NearByImageAdapter extends RecyclerView.Adapter<NearByImageAdapter.ViewHolder> implements View.OnClickListener{
    private Context context;
    private List<NearByInfoBean.BodyBean.HeadurlBean> mDatas;
    public NearByImageAdapter(Context context, List<NearByInfoBean.BodyBean.HeadurlBean> mDatas) {
        this.context = context;
        this.mDatas= mDatas;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_apply, parent, false);
        ViewHolder viewHolder=new ViewHolder(view);
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        int wh=DisplayUtil.dip2px(context,32);
        int left=DisplayUtil.dip2px(context,16);
        if(position!=0){
            RelativeLayout.MarginLayoutParams params=new RelativeLayout.MarginLayoutParams(wh, wh);
            params.setMargins(-left,0,0,0);
            holder.itemView.setLayoutParams(params);
        }else{
            RelativeLayout.MarginLayoutParams params=new RelativeLayout.MarginLayoutParams(wh, wh);
            params.setMargins(-0,0,0,0);
            holder.itemView.setLayoutParams(params);
        }
        Glide.with(context).load(mDatas.get(position).getHeadurl()).into(holder.image);
        holder.itemView.setTag(mDatas.get(position));
    }



    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public View rootView;
        public CircleImageView image;

        public ViewHolder(View rootView) {
            super(rootView);
            this.rootView = rootView;
            this.image = (CircleImageView) rootView.findViewById(R.id.image);
        }

    }

    private OnRecyclerViewItemClickListener<NearByInfoBean.BodyBean.HeadurlBean> mOnItemClickListener = null;
    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public void onClick(View v) {
        //注意这里使用getTag方法获取数据
        if(mOnItemClickListener!=null){

            mOnItemClickListener.onItemClick(v,(NearByInfoBean.BodyBean.HeadurlBean)v.getTag());
        }
    }
    public void  clear(){
        mDatas.clear();
        notifyDataSetChanged();
    }
}
